
#include "oled.h"

uint8_t OLED_RAM[8][128]={0};

void OLED_Init(){

	
	  IIC_Init();
	
	  HAL_Delay(100);

  OLED_WrCmd(0xAE);//--display off
	OLED_WrCmd(0x00);//---set low column address
	OLED_WrCmd(0x10);//---set high column address
	OLED_WrCmd(0x40);//--set start line address  
	OLED_WrCmd(0xB0);//--set page address
	OLED_WrCmd(0x81); // contract control
	OLED_WrCmd(0xFF);//--128   
	OLED_WrCmd(0xA1);//set segment remap 
	OLED_WrCmd(0xA6);//--normal / reverse
	OLED_WrCmd(0xA8);//--set multiplex ratio(1 to 64)
	OLED_WrCmd(0x3F);//--1/32 duty
	OLED_WrCmd(0xC8);//Com scan direction
	OLED_WrCmd(0xD3);//-set display offset
	OLED_WrCmd(0x00);//
	
	OLED_WrCmd(0xD5);//set osc division
	OLED_WrCmd(0x80);//
	
	OLED_WrCmd(0xD8);//set area color mode off
	OLED_WrCmd(0x05);//
	
	OLED_WrCmd(0xD9);//Set Pre-Charge Period
	OLED_WrCmd(0xF1);//
	
	OLED_WrCmd(0xDA);//set com pin configuartion
	OLED_WrCmd(0x12);//
	
	OLED_WrCmd(0xDB);//set Vcomh
	OLED_WrCmd(0x30);//
	
	OLED_WrCmd(0x8D);//set charge pump enable
	OLED_WrCmd(0x14);//
	
	OLED_WrCmd(0xAF);//--turn on oled panel
 
 
  OLED_Clr();




}



void OLED_Picture(uint8_t* pic,uint8_t height,uint8_t width,uint8_t h,uint8_t l){


     

      uint16_t i=0,j=0,num;
	    
	 
      
			for(j=0;j<width*(height/8);j++){
			
	      OLED_RAM[(h+j/width)%8][l+j%width]=*(pic++);

			}
				

		
		
		OLED_UpData();








}


void OLED_Chinese(uint8_t* str,uint8_t num,uint8_t h,uint8_t l){


	     uint8_t i=0,j=0;
	         
	
	   
	    
	  for(i=0;i<num;i++){
      
			for(j=0;j<32;j++){
			
	   OLED_RAM[(h+j/16)%8][(l+j%16)+i*16]=*(str++);
		 

			}
				

		}
		
		
		OLED_UpData();
		
		

}


void OLED_UpData(){

	 uint8_t i=0,j=0;

 for(i=0;i<8;i++){
			 
			 OLED_WrCmd(0xb0+i);
			 OLED_WrCmd(0x00);
			 OLED_WrCmd(0x10);
		 
		 for(j=0;j<128;j++){
		 
		    OLED_WrData(OLED_RAM[i][j]);
		 
		 
		 }
		 
	
		 }





}


void OLED_Str(uint8_t* str,uint8_t x,uint8_t y){


      
      uint8_t i=0,j=0;
	
	    OLED_RefreshRAM(str,x,y);
	

      OLED_UpData();
      





}



void OLED_Num(uint8_t* num,uint8_t x,uint8_t y)
{

     uint8_t i=0,len=0,j=0;
      
	    len=OLED_DataLen(num);
	  
      for(j=0;j<len;j++){
	
	    for(i=0;i<6;i++){
			
				OLED_RAM[x%8][(y+i)%128]=NumHex[*num>>4][i];
				
			  OLED_RAM[x%8][(y+i+6)%128]=NumHex[*num&0x0f][i];
				
				
			}
			
			y+=13;
	
			num++;
     
		}

    OLED_UpData();


}



uint8_t OLED_DataLen(uint8_t* data)
{
     uint8_t len=0; 
	
//	  len=sizeof(data)/sizeof(uint8_t);
	
	    while(*data!='>'){
			
				len++;
				data++;
			
			}

    return len;
		
}



void OLED_RefreshRAM(uint8_t* str,uint8_t h,uint8_t l){

      uint8_t i=0,len=0,j=0;
      
	    len=strlen((char*)str);
	  
      for(j=0;j<len;j++){
	
	    for(i=0;i<6;i++){
			
				OLED_RAM[h%8][((l+i)+(j*6))%128]=F6x8[*str-32][i];
			
			}
	
			str++;
     
		}

}


void OLED_WrCmd(uint8_t cmd){

     IIC_WrCmd(cmd);


}


void OLED_WrData(uint8_t data){

      IIC_WrData(data);


}


void OLED_LightScreen(){


     uint8_t i=0,j=0;
	
	   for(i=0;i<8;i++){
			 
			 OLED_WrCmd(0xb0+i);
			 OLED_WrCmd(0x00);
			 OLED_WrCmd(0x10);
		 
		 for(j=0;j<128;j++){
		 
		    OLED_WrData(0xff);
		 
		 
		 }
		 
	
		 }






}

void OLED_Clr(){


  uint8_t i=0,j=0;
	
	   for(i=0;i<8;i++){
			
		 
		 for(j=0;j<128;j++){
		 
		    OLED_RAM[i][j]=0x00;
		 
		 
		 }
		 
	
		 }
		 
		 



}